package leetcode;

public class LongestPalindromic {

    public static void main(String[] args) {
        LongestPalindromic l = new LongestPalindromic();

//        boolean adfga = l.judgePalindromic("cbbd", 1, 2);
//        System.out.println(adfga);
        String babad = l.longestPalindrome("aacab");
        System.out.println(babad);
    }

    public String longestPalindrome(String s) {
        String longest = "";
        int p = 0;
        while (p < s.length() - 1){
            int q = s.length() - 1;
            while(p <= q){
                int o = q;
                while(s.charAt(p) != s.charAt(o) && p >= o){
                    o --;
                }
                // 判断字符串 p - q 是不是回文
                if(judgePalindromic(s, p, o)){
                    if(longest.length() < (o - p + 1)){
                        longest = s.substring(p, o + 1);
                    }
                    break;
                }
                q --;
            }
            p ++;
        }
        return longest == "" ? String.valueOf(s.charAt(0)) : longest;
    }

    private boolean judgePalindromic(String s, int start, int end){
        while (start != end && start < s.length()&& start <= end){
            if(s.charAt(start) != s.charAt(end)){
                return false;
            }
            start ++;
            end --;
        }
        return true;
    }

}
